//
//  ViewController.m
//  17_Work Contact
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kTableView @"reuse"
#import "Contact.h"
#import "ContentViewController.h"
#import "AddViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, ContentViewControllerDelegate, AddViewControllerDelegate>
@property (nonatomic, strong) UITableView *contactTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *arrayPath;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.backgroundColor = [UIColor grayColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)];
    [self createTableView];
    [self createData];
    
}

- (void)click:(UIButton *)button {
    AddViewController *vc = [[AddViewController alloc] init];
    vc.delegate = self;
    
//    self sendAddContact:
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)sendAddContact:(Contact *)contact {
    [self.dataArray addObject:contact];
    [self.contactTableView reloadData];
    [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath];
}

- (void)createData {
    NSArray *sandArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [sandArray firstObject];
    
    self.arrayPath = [path stringByAppendingPathComponent:@"contactPerson.plist"];
    
    NSArray *unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:_arrayPath];
    if (unarchiveArray.count != 0) {
        self.dataArray = [NSMutableArray arrayWithArray:unarchiveArray];
    }
    else {
        Contact *contact1 = [[Contact alloc] initWithPicImage:@"u=3808451166,1723806820&fm=23&gp=0.jpg" name:@"Eason" phoneNum:@"123456" address:@"香港"];
        self.dataArray = [NSMutableArray array];
        [_dataArray addObject:contact1];
        [NSKeyedArchiver archiveRootObject:_dataArray toFile:_arrayPath];
    }
    NSLog(@"%@", _arrayPath);
}


- (void)createTableView {
    self.contactTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.contactTableView.delegate = self;
    self.contactTableView.dataSource = self;
    [self.view addSubview:self.contactTableView];
    [self.contactTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableView];
}

// 实现删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [self.contactTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath];
    [self.contactTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView forIndexPath:indexPath];
    cell.textLabel.text = [self.dataArray[indexPath.row] name];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ContentViewController *vc = [[ContentViewController alloc] init];
    vc.delegate = self;
    vc.index = indexPath.row;
    vc.contact = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)sendContact:(Contact *)contact index:(NSInteger)index {
    [self.dataArray removeObjectAtIndex:index];
    [self.dataArray insertObject:contact atIndex:index];
    [self.contactTableView reloadData];
    [NSKeyedArchiver archiveRootObject:self.dataArray toFile:self.arrayPath];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
